package org.example.demo1;

import com.google.gson.Gson;
import dao.StudentDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

@WebServlet(name = "DeleteStu", value = "/delstu")
public class DeleteStu  extends HttpServlet {  //servlet

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setCharacterEncoding("utf-8");
       req.setCharacterEncoding("utf-8");
        //1根据主键删除  sid
        String sid = req.getParameter("sid");  //获取要删除的数据
        //2 调用dao操作
        StudentDao studao = new StudentDao();
            int flag=studao.delStuById(sid);
         HashMap<String, Object> hs = new HashMap<>();
         if (flag>0){
             hs.put("code",200);
             hs.put("msg","删除成功");
             hs.put("data",1);
         }else {
             hs.put("code",500);
             hs.put("msg","删除失败");
         }
         Gson gson = new Gson();
         String json = gson.toJson(hs);
         resp.getWriter().println(json);
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }
}